using System;
using System.Collections.Generic;
using System.Linq;
using GameFrameworkLibraly.BaseObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameFrameworkLibraly.Controls
{
    public class RadioButtonListControl:Control
    {
        private readonly List<RadioButtonControl> _radioButtonControls;

        /// <summary>
        /// Creates a new instance of RadioButtonListControl. Only 1 radiobox can be selected at any moment
        /// </summary>
        /// <param name="game">Current game</param>
        /// <param name="radioButtonControls">List of radio buttons</param>
        public RadioButtonListControl(MyGame game, List<RadioButtonControl> radioButtonControls):base(game)
        {
            SelctedIndex = -1;
            _radioButtonControls = radioButtonControls;
            foreach (var radioButtonControl in _radioButtonControls)
            {
                radioButtonControl.ItemSelected += RadioButtonControlItemSelected;
            }
        }

        /// <summary>
        /// Returns selected item
        /// </summary>
        public RadioButtonControl SelctedItem { get; private set; }

        /// <summary>
        /// Returns index of selected item
        /// </summary>
        public int SelctedIndex { get; private set; }

        /// <summary>
        /// Occures when selected item has been changed
        /// </summary>
        public event EventHandler SelectedItemChanged;

        #region Control

        /// <summary>
        /// Draws the control
        /// </summary>
        /// <param name="spriteBatch"></param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            if (!Visible)
                return;
            if(BackgroundTexture!=null)
                spriteBatch.Draw(BackgroundTexture,DestinationRectangle,BackColorFilter);
            foreach (var radioButtonControl in _radioButtonControls)
            {
                radioButtonControl.Draw(spriteBatch);
            }
        }

        /// <summary>
        /// Updates state of the control
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            if(!Enabled)
                return;


            int heightForAnItem = DestinationRectangle.Height / _radioButtonControls.Count;

            for (int i = 0; i < _radioButtonControls.Count; i++)
            {
                var radioButtonControl = _radioButtonControls[i];
                if (!DestinationRectangle.IsEmpty)
                {
                    radioButtonControl.DestinationRectangle = new Rectangle(DestinationRectangle.X,
                                                                            DestinationRectangle.Y + i*heightForAnItem,
                                                                            DestinationRectangle.Width, heightForAnItem);
                }
                radioButtonControl.Update(gameTime);
            }
        }

        /// <summary>
        /// Handles a touch and changes the state of this component if needed
        /// </summary>
        /// <param name="touchLocation">Touch Location</param>
        /// <returns>True, if touch was handled</returns>
        public override bool ProcessTouch(Microsoft.Xna.Framework.Input.Touch.TouchLocation touchLocation)
        {
            if (!Enabled)
                return false;
            return _radioButtonControls.Any(radioButtonControl => radioButtonControl.ProcessTouch(touchLocation));
        }

        public override bool ProcessGesture(Microsoft.Xna.Framework.Input.Touch.GestureSample gestureSample)
        {
            if (!Enabled)
                return false;
            return _radioButtonControls.Any(radioButtonControl => radioButtonControl.ProcessGesture(gestureSample));
        }

        #endregion


        private void RadioButtonControlItemSelected(object sender, EventArgs e)
        {
            for (int i = 0; i < _radioButtonControls.Count; i++)
            {
                var radioButtonControl = _radioButtonControls[i];
                if (radioButtonControl == sender)
                {
                    radioButtonControl.SetItemChecked(true);
                    SelctedItem = (RadioButtonControl) sender;
                    SelctedIndex = i;
                    InvokeSelectedItemChanged(EventArgs.Empty);
                }
                else
                    radioButtonControl.Checked = false;
            }
        }

        private void InvokeSelectedItemChanged(EventArgs e)
        {
            EventHandler handler = SelectedItemChanged;
            if (handler != null) handler(this, e);
        }
    }
}
